<?php

/**
 * @author Edison P. Quinones Jr
 * @version  1.0.0
 * @copyright 2008
 */

class IndexController extends Zend_Controller_Action
{
	function init()
	{
		$this->initView();
		$this->view->baseUrl = $this->_request->getBaseUrl();
		$webInfoNamespace  = new Zend_Session_Namespace('webInfo');
		$this->view->webInfo = $webInfoNamespace->webInfo;
		Zend_Loader::loadClass('Zend_Filter_StripTags');
		Zend_Loader::loadClass('Zend_Validate_EmailAddress');
		Zend_Loader::loadClass('Zend_Validate_StringLength');
		Zend_Loader::loadClass('JumperAccount');
		Zend_Loader::loadClass('ConfirmationList');
		Zend_Loader::loadClass('Defaultprofile');
		Zend_Loader::loadClass('GroupjumpNews');
		Zend_Loader::loadClass('WidgetDisplay');
		Zend_Loader::loadClass('JumpsiteDisplayInThumbnails');

		//Zend_Loader::loadClass('HeaderLink');

		$countWords = strlen($_SERVER['REQUEST_URI']);
		if (strpos($_SERVER['REQUEST_URI'], '/', $countWords-1) == true)
		{
			$this->_redirect(substr_replace($_SERVER['REQUEST_URI'], '', $countWords-1));
			//$this->view->testingLang = substr_replace($_SERVER['REQUEST_URI'], '', $countWords-1);
		}

		$this->view->user = Zend_Auth::getInstance()->getIdentity();
		
		# Links
		$registry = Zend_Registry::getInstance();
//		$arrayLinksNamespace = new Zend_Session_Namespace('arrayLinks');
//		$arrayLinks = $arrayLinksNamespace->arrayLinksConfig;
		$this->view->links = $registry->get('linksArray');//$arrayLinks;
		# end of links
	}
	function indexAction()
	{
		
		
		//Header
		$this->view->keywords = 'Groupjump, Jumpsite, Group, Social, weblets, nextix';
		$this->view->description = 'Groupjump is new social technology where a group can have its own jumpsite';
		$this->view->author = 'Edison Quinones Jr. and Vince Loremia';
		$this->view->robots = 'Index';
		$this->view->title = 'Groupjump, Your World on the web.Jump in!';		

		$this->view->PageContentTitle = 'leftLabelFeaturedGroup';

		//Page Located
		$this->view->stringToCompareWithTheLink = 'Start';

		$groupjump_news = new GroupjumpNews();		
		$this->view->groupJumpNewsFeeds =  $groupjump_news->fetchAll('id <> 0','id desc',2);
				
		$widgetDisplay = new WidgetDisplay();
		$this->view->featuredWidget = $widgetDisplay->fetchAll('id <> 0','id desc',3);
		//fetchAll($where, $order, $limit, $offset);
		
		$groupNameThumbnails = array();
		$groupDescriptionThumbnails = array();
		$groupThumbnailLinks = array();
		
		array_push($groupNameThumbnails, "GroupTest nail");
		array_push($groupDescriptionThumbnails,
		"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Vivamus dapibus, lorem in sodales iaculis, quam libero eleifend metus, eu mollis nibh tortor vel ante.");
		array_push($groupThumbnailLinks, "jumpsitesampple.php");
		
		$jumspiteDisplay = new JumpsiteDisplayInThumbnails();
		$this->view->jumpsiteDisplay = $jumspiteDisplay->getGroupWithAvatarLimit9();
		
		/*array(
		array('featuredWidgetName' => 'Widget name 1', 'featuredWidgetLinks' => 'widget/2342', 'featuredWidgetThumbnail' => 'thumbnailTest1.jpg', 'featuredWidgetDateAdded' => '02.24.08', 'featuredWidgetDescription' => 'Ambot lang jud kung mag buhat gani og template layout kay latin jud nah ang gamiton sa content.  Ngano dili man bisaya. Mas nice man bisaya kay gamay ray kahibaw.  Then minus makasabot.  Then mas katoanan siya paminawon.  Mas nice pa jud kay proud ka sa imong language.  Kung mahimo himoon pa jud ni og tagalog para tanan sa pilipinas makasabot.'),
		array('featuredWidgetName' => 'Widget name 2', 'featuredWidgetLinks' => 'widget/2342', 'featuredWidgetThumbnail' => 'thumbnailTest1.jpg', 'featuredWidgetDateAdded' => '02.24.08', 'featuredWidgetDescription' => 'Ambot lang jud kung mag buhat gani og template layout kay latin jud nah ang gamiton sa content.  Ngano dili man bisaya. Mas nice man bisaya kay gamay ray kahibaw.  Then minus makasabot.  Then mas katoanan siya paminawon.  Mas nice pa jud kay proud ka sa imong language.  Kung mahimo himoon pa jud ni og tagalog para tanan sa pilipinas makasabot.'),
		array('featuredWidgetName' => 'Widget name 2', 'featuredWidgetLinks' => 'widget/2342', 'featuredWidgetThumbnail' => 'thumbnailTest1.jpg', 'featuredWidgetDateAdded' => '02.24.08', 'featuredWidgetDescription' => 'Ambot lang jud kung mag buhat gani og template layout kay latin jud nah ang gamiton sa content.  Ngano dili man bisaya. Mas nice man bisaya kay gamay ray kahibaw.  Then minus makasabot.  Then mas katoanan siya paminawon.  Mas nice pa jud kay proud ka sa imong language.  Kung mahimo himoon pa jud ni og tagalog para tanan sa pilipinas makasabot.'),
		);*/


		//featured groups
		/*groupNameThumbnails = array();
		$groupDescriptionThumbnails = array();
		$groupThumbnailLinks = array();

		array_push($groupNameThumbnails, "GroupTest nail");
		array_push($groupDescriptionThumbnails,
		"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Vivamus dapibus, lorem in sodales iaculis, quam libero eleifend metus, eu mollis nibh tortor vel ante.");
		array_push($groupThumbnailLinks, "jumpsitesampple.php");*/


	}
	function  errorAction()
	{

		//Header
		$this->view->keywords = 'Edison, Quinones, Jr';
		$this->view->description = 'This is the philippines international airport';
		$this->view->author = 'Edison P. Quinones Jr.';
		$this->view->robots = 'Index';
		$this->view->title = 'The title of the philippines';

		$this->view->PageContentTitle = 'leftLabel';

		//Page Located
		$this->view->stringToCompareWithTheLink = 'Root';

		$defaultNamespace  = new Zend_Session_Namespace('Default');
		$this->view->uriSession = $defaultNamespace->uriSession;
	}

	function loginAction()
	{
		Zend_Loader::registerAutoload();
		$filter = new Zend_Filter_StripTags();
		$email = trim($filter->filter($this->_request->getPost('loginUsername')));
		$password = trim($filter->filter($this->_request->getPost('loginPassword')));

		# check if user flag_confirm is equal to zero
		$jumper_account = new JumperAccount();
		# end of checking user flag_confirm is equal to zero

		if (empty($email))
		{
			$this->view->json_send = "{ success: false, errors: { reason: 'Please provide a username.' }}";
		}
		else
		{
			// setup Zend_Auth adapter for a database table
			$db = Zend_Registry::get('db');
			//$authAdapter = new Zend_Auth_Adapter_DbTable($db);
			$authAdapter = new Zend_Auth_Adapter_DbTable($db, 'jumper_account', 'email_add', 'password', 'SHA1(?)');
			$authAdapter->setTableName('jumper_account');
			$authAdapter->setIdentityColumn('email_add');
			$authAdapter->setCredentialColumn('password');
			// Set the input credential values to authenticate against
			$authAdapter->setIdentity($email);
			$authAdapter->setCredential($password);
			// do the authentication
			$auth = Zend_Auth::getInstance();
			$result = $auth->authenticate($authAdapter);

			switch ($result->getCode())
			{
				case Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND:
					$this->view->json_send = "{ success: false, errors: { reason: 'Email account is not registered.' }}";
					break;

				case Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID:
					$this->view->json_send = "{ success: false, errors: { reason: 'Password is incorrect.' }}";
					break;

				case Zend_Auth_Result::SUCCESS:
					$data = $authAdapter->getResultRowObject(null,'password');
					$auth->getStorage()->write($data);
					$this->view->json_send = "{ success: true}";
					break;

				default:
					$this->view->json_send = "{ success: false, errors: { reason: 'Email and Password invalid.' }}";
					break;
			}
		}

		$this->view->title = "Log in";
	}
	function logoutAction()
	{
		Zend_Auth::getInstance()->clearIdentity();
		$this->_redirect('/');
	}
}